package com.cgsd.heyu.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import com.android.library.utils.FileUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.cgsd.heyu.BuildConfig;

import java.io.File;
import java.util.List;

/**
 * @author xiaoxing
 * Description 保存数据
 * CreateData: 2020/11/5
 */
public class SaveFileUtil {
    /**
     * 文件前缀
     */
    public static final String FILE_NAME_PREFIX = "era_custom";

    public static void saveBitmap(Context context,List<String> urls){
        for(String url : urls) {
            Glide.with(context).asBitmap().load(url).into(new CustomTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    String fileName = FILE_NAME_PREFIX + System.currentTimeMillis();
                    FileUtils.saveBitmap(resource,fileName,context);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            });
        }
        
    }

    /**
     * 保存图片到相册
     * @param context
     * @param bitmap
     */
    public static void saveBitmap(Context context,Bitmap bitmap){
        String fileName = FILE_NAME_PREFIX + System.currentTimeMillis();
        FileUtils.saveBitmap(bitmap,fileName,context);
    }


    /**
     * 获取指定文件uri
     * @param file
     * @return
     */
    public static Uri getFileUri(Context context,File file){
        //判断是否是AndroidN以及更高的版本
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() +".fileprovider", file);
        }else {
            uri = Uri.fromFile(file);
        }
        return uri;
    }

    public static void installApk(String apkName,Context mContext) {
        File apkfile = new File(apkName);
        if (!apkfile.exists()) {
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri contentUri = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".fileprovider", apkfile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
            //兼容8.0
            if (android.os.Build.VERSION.SDK_INT >= 26) {
                boolean hasInstallPermission = mContext.getPackageManager().canRequestPackageInstalls();
                if (!hasInstallPermission) {
                    //请求安装未知应用来源的权限
                    ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES}, 6666);
                }
            }
        } else {
            // 通过Intent安装APK文件
            intent.setDataAndType(Uri.parse("file://" + apkfile.toString()),
                    "application/vnd.android.package-archive");
        }
        if (mContext.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
            mContext.startActivity(intent);
        }
    }
}
